import HeadPrefab from "../../../component/HeadPrefab";
import ComponentBase from "../../../engine/ComponentBase";
import DateUtil from "../../../engine/DateUtil";
import { EventManager } from "../../../engine/EventManager";
import { Logger } from "../../../engine/Logger";
import { SpriteFrameComponent } from "../../../engine/SpriteFrameComponent";
import UIManager from "../../../engine/UIManager";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { EventConfig } from "../../config/EventConfig";
import { ViewConfig } from "../../config/ViewConfig";
import RedDotManager from "../../manager/RedDotManager";
import SettingModel from "../../model/SettingModel";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";
import { ViewCheck } from "../ViewCheck";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubTopUI extends ComponentBase {

    private btnClubManager: cc.Node
    private btnExitClub: cc.Node
    private qunzhu: cc.Sprite

    onEnable(): void {
        super.onEnable()
        EventManager.instance.on(EventConfig.Event_Update_UserInfo, this.refresh, this)
        let btnBack: cc.Node = this.findChild(this.node, "btnBack")
        UIHelper.onAddEvent(btnBack, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewClubList, () => {
                UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewClubHall)
            })
        })
        this.qunzhu = this.getChild(this.node, "qunzhu", cc.Sprite)

        let head: HeadPrefab = this.getChild(this.node, "head", HeadPrefab)
        head.setHead(UserModel.instance.userPhoto);
        UIHelper.onAddEvent(head.node, () => {
            // UIManager.instance.showView(ViewConfig.viewConfig.ViewUserInfo)
        })
        let txtName: cc.Label = this.getChild(this.node, "txtName", cc.Label)
        txtName.string = UserModel.instance.userName
        let txtClubName: cc.Label = this.getChild(this.node, "txtClubName", cc.Label)
        txtClubName.string = UserModel.instance.nowClub.clubName

        let txtId: cc.Label = this.getChild(this.node, "txtId", cc.Label);
        txtId.string = "" + UserModel.instance.nowClub.clubid + ""
        this.btnClubManager = this.findChild(this.node, "topControlNode/btnClubManager")
        RedDotManager.instance.registerReddot(RedDotManager.RedDot_Type.Club_shenhe, this.btnClubManager.getChildByName("ui_hongdian"))
        this.btnExitClub = this.findChild(this.node, "topControlNode/btnExitClub")
        UIHelper.onAddEvent(this.btnClubManager, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewClubManager)
        })
        UIHelper.onAddEvent(this.btnExitClub, () => {
            UIHelper.showCheck("您确定要退出亲友圈吗？", (isSuc: boolean) => {
                if (isSuc) {
                    ClubSocketClient.req_club_exit(UserModel.instance.nowClub.clubid, () => {
                        UIManager.instance.showView(ViewConfig.viewConfig.ViewClubList, () => {
                            UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewClubHall)
                        })
                    })
                }
            }, ViewCheck.Dialog_Type.All)
        })
        let btnNotice: cc.Node = this.findChild(this.node, "btnNotice")
        UIHelper.onAddEvent(btnNotice, () => {
            // UIManager.instance.showView(ViewConfig.viewConfig.ViewClubNotice)
        })
        if (!DateUtil.isToday(SettingModel.instance.lastClubNoticeTime)) {
            SettingModel.instance.lastClubNoticeTime = DateUtil.now()
            SettingModel.instance.saveToLocal()
            // UIManager.instance.showView(ViewConfig.viewConfig.ViewClubNotice)
        }
        let btnMember: cc.Node = this.findChild(this.node, "btnMember")
        UIHelper.onAddEvent(btnMember, () => {
            if (UserModel.instance.nowClub.actor != ClubConfig.Club_Actor_Type.general) {

            }

        })
        let btnCreate: cc.Node = this.findChild(this.node, "btnCreate")
        UIHelper.onAddEvent(btnCreate, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewCreateRoom)
        })
        let btnZhanji: cc.Node = this.findChild(this.node, "btnZhanji")
        UIHelper.onAddEvent(btnZhanji, () => {
            // UIManager.instance.showView(ViewConfig.viewConfig.ViewGameRecord)
        })
       
        this.refresh()
    }

    private refresh() {
        let txtZuanshi: cc.Label = this.getChild(this.node, "txtZuanshi", cc.Label)
        txtZuanshi.string = UserModel.instance.diamond + ""
        this.btnExitClub.active = true
        if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.general) {
            this.btnClubManager.active = false;
            this.qunzhu.spriteFrame = this.qunzhu.getComponent(SpriteFrameComponent).spriteFrameList[2]
        }
        else {
            this.btnClubManager.active = true;
            if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.owner) {
                this.btnExitClub.active = false
                this.qunzhu.spriteFrame = this.qunzhu.getComponent(SpriteFrameComponent).spriteFrameList[0]
            } else if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.administrator) {
                this.qunzhu.spriteFrame = this.qunzhu.getComponent(SpriteFrameComponent).spriteFrameList[1]
            }
        }
    }

}
